package com.xnew.org.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import com.xnew.org.mapper.OrgMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
@CacheConfig
@Transactional(rollbackFor = Exception.class)
public class OrgService {
	@Autowired
	private OrgMapper orgMapper;

	public List<Map<String, Object>> createOrgTree() {
		List<Map<String, Object>> orgList = new ArrayList<>();
		Map<String, Object> orgMap = new HashMap<String, Object>();
		orgList = orgMapper.querySysOrgList(orgMap);

		// 最后的结果
		List<Map<String, Object>> orgAllList = new ArrayList<Map<String, Object>>();
		// 先找到所有的一级
		for (int i = 0; i < orgList.size(); i++) {
			// 一级菜单parentId为0
			if ("0".equals(orgList.get(i).get("PARENT_ID"))) {
				orgAllList.add(orgList.get(i));
			}
		}
		// 设置子集，getorgChild是递归调用的
		for (Map<String, Object> map : orgAllList) {
			map.put("children", this.getOrgChild(map.get("ORG_ID").toString(), orgList));
		}
		return orgAllList;
	}


	/**
	 * 递归查找子集
	 *
	 * @return
	 */
	private List<Map<String, Object>> getOrgChild(String orgId, List<Map<String, Object>> orgList) {
		List<Map<String, Object>> childList = new ArrayList<>();
		for (Map<String, Object> map : orgList) {
			if (map.get("PARENT_ID").toString().equals(orgId)) {
				childList.add(map);
			}
		}
		for (Map<String, Object> map : childList) {
			map.put("children", this.getOrgChild(map.get("ORG_ID").toString(), orgList));
		}
		if (childList.size() == 0) {
			return null;
		}
		return childList;
	}

	public List<Map<String, Object>> createOrgTreeByOrgId(String orgId) {
		List<Map<String, Object>> orgList = new ArrayList<>();
		Map<String, Object> orgMap = new HashMap<String, Object>();
		orgMap.put("orgId", orgId);
		orgList = orgMapper.querySysOrgList(orgMap);

		// 最后的结果
		List<Map<String, Object>> orgAllList = new ArrayList<Map<String, Object>>();
		// 先找到所有的一级
		for (int i = 0; i < orgList.size(); i++) {
			// 一级菜单parentId为0
			if ("0".equals(orgList.get(i).get("PARENT_ID"))) {
				orgAllList.add(orgList.get(i));
			}
		}
		// 设置子集，getorgChild是递归调用的
		for (Map<String, Object> map : orgAllList) {
			map.put("children", this.getOrgChild(map.get("ORG_ID").toString(), orgList));
		}
		return orgAllList;
	}

	public List<Map<String, Object>> queryFirstNodeByOrgId(String orgId) {
		List<Map<String, Object>> orgList = new ArrayList<>();
		Map<String, Object> orgMap = new HashMap<String, Object>();
		orgMap.put("orgId", orgId);
		orgList = orgMapper.queryFirstNodeByOrgId(orgMap);

		// 最后的结果
		List<Map<String, Object>> orgAllList = new ArrayList<Map<String, Object>>();
		// 先找到所有的一级
		for (int i = 0; i < orgList.size(); i++) {
			// 一级菜单parentId为0
			if ("0".equals(orgList.get(i).get("PARENT_ID"))) {
				orgAllList.add(orgList.get(i));
			}
		}
		// 设置子集，getorgChild是递归调用的
		for (Map<String, Object> map : orgAllList) {
			map.put("children", this.getOrgChild(map.get("ORG_ID").toString(), orgList));
		}
		return orgAllList;
	}
}
